package com.froggyware.tadpole.core.impl;

import com.froggyware.tadpole.TadObject;
import com.froggyware.tadpole.core.DataStore;
import com.froggyware.tadpole.util.FileSystem;
import de.undercouch.bson4jackson.BsonFactory;
import de.undercouch.bson4jackson.BsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;
import org.xerial.snappy.SnappyOutputStream;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Steven
 * Date: 10/09/11
 * Time: 15:54
 * To change this template use File | Settings | File Templates.
 */
public class BsonDiskDataStore extends AbstractDataStore {

    @Override
    public void write(TadObject t) {

        // Optimized streaming
        BsonFactory fac = new BsonFactory();
        fac.enable(BsonGenerator.Feature.ENABLE_STREAMING);

        ObjectMapper mapper = new ObjectMapper(fac);

        String directory = getFileDirectory(t);

        try {
            if(enableCompression) {
                OutputStream f0 = new FileOutputStream(directory + File.separator + "user."+t.getId()+".json");
                SnappyOutputStream s = new SnappyOutputStream(f0);
                mapper.writeValue(s,t);
            } else {
                mapper.writeValue(new File(directory + File.separator + "user."+t.getId()+".json"),t);
            }

        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

    @Override
    public TadObject read(String objectId, Class c) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<String> getCollectionNames() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<String> getObjectIds(String collectionName) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void drop() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void delete(String key, Class clazz) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
